<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">
    $(document).ready(function() {
        var fm = ["pingForm", "child_container", "pingFormId"];
        Page.createForm(fm);
        TempUtil.render('child_template', 'pingForm', DOC);
        Page.setStripeTable();
		var $btnSave = $('#btnSave'),
			  $pinTypes = $('input[name=radPins]'),
			  $pinStatus = $('#pinStatus');
		var $box = $('#edit_box2'), $mask = $('#mask');
		var pingType = 0,ifNeedRestart = true;
            Page.postJSON({
                json: {
                    cmd: RequestCmd.QUERY_SIM_STATUS
                },
                success: function(datas) {
					 if(datas.sim != ''){
						if(datas.sim.card_status == '1'){
                            pingType = "1";
                            ifNeedRestart = false;
                            $("input[name='radPins']").disable();
                        }else if(datas.sim.card_status == '2'){
                           $(".is_show_puk").show(0);
                           $("#trPinOper").hide(0);
                           $(".is_show_puk_hide").hide(0);
                           $(".is_change_pin").hide(0);
                           $("#trPinStatus").hide(0);
                           pingType = "2";
                        }else if(datas.sim.card_status == '0'){
                            $(".is_show_puk").hide(0);
                            pingType = "3";
                            if(datas.sim.pinlock_enable.trim() == "1"){
                                $pinStatus.html(DOC.status.opened);
                                $("#spanChangePin").show(0);
                                $("input[value='1']").prop("checked",true);
                            }else{
                                $("input[value='2']").prop("checked",true);
                                $pinStatus.html(DOC.status.closed);
                            }
                        }
                    }else{
                        $("input[name='radPins']").disable();
						$pinStatus.html(DOC.lbl.noSim);
					}
                }
            });
       
    		$("input[name='radPins']").change(function(){
                if(this.value == "3"){
                    $(".is_change_pin").show(0);
                }else{
                    $(".is_change_pin").hide(0);
                }
            })
		 $btnSave.click(function () {
			     var json = {};
                 $btnSave.disable();
                 json.type = $("input[name='radPins']:checked").val();
                 json.pingType = pingType;
                 json.new_pin = $("#pinPasswordConfirm").val();
                 json.passwd = $('#pinPassword').val();
                 json.pukPassword = $("#pukPassword").val();
                 json.pukPinPasswordConfirm = $("#pukPinPasswordConfirm").val();
                 if(json.type == "3"){
                    if($("#pinPassword").val().length < 1 || $("#pinPasswordChange").val().length < 1){
                        AlertUtil.alertMsg(DOC.status.repPinCheck);
                        $btnSave.enable();
                        return false;
                    }
                    if($("#pinPasswordChange").val() != json.new_pin){
                        AlertUtil.alertMsg(CHECK.required.newPinConfirm2);
                        $btnSave.enable();
                        return false;
                    }
                 }
                 if(pingType == "2"){
                    if($("#pukPinPassword").val().length < 1){
                        AlertUtil.alertMsg(DOC.status.repPinCheck);
                        $btnSave.enable();
                        return false;
                    }

                    if($("#pukPinPassword").val() != $("#pukPinPasswordConfirm").val()){
                        AlertUtil.alertMsg(CHECK.required.newPinConfirm2);
                        $btnSave.enable();
                        return false;
                    }
                 }
                 json.method = JSONMethod.POST;
                 json.cmd = RequestCmd.SET_PIN_STATUS;
				 Page.postJSON({
                     json: json,
                     success: function(data) {
                        $btnSave.enable();
                        //  $("ul.tabs a").eq(5).click();
                         if(ifNeedRestart == false){
                            AlertUtil.alertMsg(PROMPT.saving.success);
                         }else{
                            SysUtil.rebootDevice(PROMPT.tips.rebootMessage);
                         }  
                     },
					 fail: function(data) {
                        $btnSave.enable();
                            Page.postJSON({
                                json: {
                                    cmd: RequestCmd.QUERY_SIM_STATUS
                                },
                            success: function(datas) {
                                //  $("ul.tabs a").eq(5).click();
                                var pin_left_times = datas.sim.pin_left_times;
                                if(ifNeedRestart == false){
                                     AlertUtil.alertMsg(PROMPT.saving.pingTimes.format(parseInt(pin_left_times)));
                                 }else{
                                    if(parseInt(pin_left_times)>0){
                                       AlertUtil.alertMsg(PROMPT.saving.pingTimes.format(parseInt(pin_left_times)));
                                    }else{
                                        AlertUtil.alertMsg(PROMPT.saving.pingTimes.format(parseInt(pin_left_times)));
                                    }
                                 }
                               
                            }
                        });                        
                     }
                 });
             

        })

    });
</script>

<div class="tab_box" id="child_container">
    <div id="pingFormId">
        <script type="text/template" id="child_template">
            <div>
                <!-- <form id="pingForm" > -->
                <table class="detail" cellspacing="0">
                    <tr id="trPinStatus" class="pins">
                        <th><%- lbl.pinVerification %><%- colon %></th>
                        <td><span id="pinStatus"></span></td>
                    </tr>
                    <tr id="trPinOper" class="pins">
                        <th><%- lbl.pinOper %><%- colon %></th>
                        <td>
                            <span id="spanOpenPin"><input type="radio" name="radPins" value="1" /><%- lbl.pinOper1 %>&nbsp;&nbsp;</span>
                            <span id="spanClosePin"><input type="radio" name="radPins" value="2" /><%- lbl.pinOper2 %>&nbsp;&nbsp;</span>
                            <span id="spanChangePin" style="display:none;"><input type="radio" name="radPins" value="3" /><%- lbl.pinOper3 %>&nbsp;&nbsp;</span>
                        </td>
                    </tr>
                    <tr class="is_show_puk_hide">
                    <th><span><%-lbl.pinCode%><%- colon %></span></th>
                    <td><input type="text" id="pinPassword" name="pinPassword"><span class="cmt"><%- TAB.basic.pinSetRemind %></span></td>
                    </tr>
                    <tr class="is_change_pin" style="display:none;">
                    <th><span><%-DOC.lbl.pinOper3%><%- colon %></span></th>
                    <td><input type="text" id="pinPasswordChange" name="pinPasswordChange"></td>
                    </tr>
                    <tr class="is_change_pin" style="display:none;">
                    <th><span><%-DOC.lbl.pinchange%><%- colon %></span></th>
                    <td><input type="text" id="pinPasswordConfirm" name="pinPasswordConfirm"></td>
                    </tr>

                    </tr>
                    <tr class="is_show_puk" style="display:none;">
                    <th><span><%-CHECK.required.puk%><%- colon %></span></th>
                    <td><input type="text" id="pukPassword" name="pukPassword"></td>
                    </tr>
                    <tr class="is_show_puk" style="display:none;">
                    <th><span><%-CHECK.required.newPin%><%- colon %></span></th>
                    <td><input type="text" id="pukPinPassword" name="pukPinPassword"></td>
                    </tr>
                    <tr class="is_show_puk" style="display:none;">
                    <th><span><%-CHECK.required.newPinConfirm%><%- colon %></span></th>
                    <td><input type="text" id="pukPinPasswordConfirm" name="pukPinPasswordConfirm"></td>
                    </tr>

                    <tr>
                        <th>&nbsp;</th>
                        <td><input type="button" id="btnSave" value="<%- btn.set %>" /></td>
                    </tr>
                </table>    
                <!-- </form> -->
            </div>
        </script>
    </div>
</div>

